<?php
class Mcategory extends CI_Model {
    protected $_table;
    public function __construct() {
        parent::__construct();
        $this->_table="categories";
    }
    
    public function listAll() {
        $query=$this->db->query("SELECT c.cate_id, c.cate_title, ca.cate_title AS parent, c.cate_parent, c.module_id, c.cate_order, c.cate_note, m.module_name
                                    FROM categories AS c
                                    LEFT OUTER JOIN categories AS ca ON ca.cate_id = c.cate_parent
                                    INNER JOIN modules AS m ON m.module_id = c.module_id order by parent, cate_order");
        if($query->num_rows()>0) return $query->result_array(); else return false;
    }
    
    public function listAllLimit($amount, $start) {
        $query=$this->db->query("SELECT c.cate_id, c.cate_title, ca.cate_title AS parent, c.cate_parent, c.module_id, c.cate_order, c.cate_note, m.module_name
                                    FROM categories AS c
                                    LEFT OUTER JOIN categories AS ca ON ca.cate_id = c.cate_parent
                                    INNER JOIN modules AS m ON m.module_id = c.module_id order by parent, cate_order limit $start, $amount");
        if($query->num_rows()>0) return $query->result_array(); else return false;
    }
    
    public function getById($id) {
        $this->db->where("cate_id", $id);
        $query=$this->db->get($this->_table);
        if($query->num_rows()>0) return $query->row_array(); else return false;
    }
    
    public function getByCateParentAndModuleId($module_id, $cate_parent='0') {
        $this->db->where("module_id", $module_id);
        $this->db->where("cate_parent", $cate_parent);
        $query=$this->db->get($this->_table);
        if($query->num_rows()>0) return $query->row_array(); else return false;
    }
    
    public function countAll() {
        return $this->db->count_all($this->_table);
    }
    
    public function search($title, $parent, $module, $amount, $start) {
        $t=$p=$m="'1=1'";
        if($title!="") $t="c.cate_title like '%".$title."%'";
        if($parent!="") $p="c.cate_parent = '".$parent."'";
        if($module!="") $m="c.module_id = '".$module."'";
        $str="select c.*, ca.cate_title as parent, m.module_name from categories as c left outer join categories as ca on ca.cate_id=c.cate_parent inner join modules as m on m.module_id=c.module_id where $t and $p and $m limit $start, $amount";
        $query=$this->db->query($str);
        if($query->num_rows()>0) return $query->result_array(); else return false;
    }
    
    public function countAllResults($title, $parent, $module) {
        $t=$p=$m="'1=1'";
        if($title!="") $t="cate_title like '%".$title."%'";
        if($parent!="") $p="cate_parent like '%".$parent."%'";
        if($module!="") $m="module_id = '".$module."'";
        $str="select count(*) as 'count' from categories where $t and $p and $m";
        $query=$this->db->query($str);
        if($query->num_rows()>0) return $query->row_array(); else return false;
    }
    
    public function insert($data) {
        $this->db->insert($this->_table, $data);
    }
    
    public function update($data, $id) {
        $this->db->where("cate_id", $id);
        $this->db->update($this->_table, $data);
    }
    
    public function del($id) {
        $this->db->where("cate_id", $id);
        $this->db->delete($this->_table);
    }
    
    public function checkTitle($title, $id="") {
        if($id!="")
            $this->db->where("cate_id != ", $id);
        $this->db->where("cate_title", $title);
        $query=$this->db->get($this->_table);
        if($query->num_rows()>0)
            return false;
        else
            return true;
    }
}
?>
